import common.CommonFunction;
import common.Mask;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;

public var custList:ArrayCollection;
public var reqproXML:XML;
public function successon(event:ResultEvent):void
{
	Mask.close();
	reqproXML = event.result as XML;
	custList = new ArrayCollection();
	for each(var obj:Object in reqproXML.record)
	{
		custList.addItem({select:true, area:obj.code, description:obj.description});
	}
	retyp.dataProvider = custList;
	retyp.labelField = "description";
}

public function onfail(event:FaultEvent):void
{
	Mask.close();
	Alert(event.message + "");
	
}
public var infoObject:Object;
public var info:Object;
public function onCreationComplete():void
{
	infoObject = new Object();
	infoObject["date"] = new Date();
	getData.send(infoObject);
	Mask.show(CommonFunction.getMessages('Please Wait..'));
}

public var customerXML:XML;
public var customerList:ArrayCollection;
public function onSuccess(event:ResultEvent):void
{
	Mask.close();
	customerXML = event.result as XML;
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, area:obj.code, extnsion_num:obj.description});
	}
	combo.dataProvider = customerList;
	combo.labelField = "extnsion_num";
	info = new Object();
	info["date"] = new Date();
	proedit.send(info);
}

public function onfailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(event.message + "");
}

protected function combo_changeHandler(event:ListEvent):void
{
	if(combo.selectedItem.area =="01")	
	{
		profilee.visible=true;
		predit.visible=false;
		statere.dataProvider = groupDB.country.(@name == "India").state.@name;
	}
	if(combo.selectedItem.area=="02")
	{
		predit.visible=true;
		profilee.visible=false;	
	}
}
protected function subreq_clickHandler(event:MouseEvent):void
{
	info = new Object();
	info["date"] = new Date();
	info["mb"]=mobile.text;
	info["cn"]=cname.text;
	info["ad"]=address.text;
	info["ci"]=cityre.text;
	info["st"]=statere.text;
	info["na"]=nar.text;
	info["e"]=emil.text;
	senddata.send(info);
	
	Mask.show(CommonFunction.getMessages('Please Wait..'));
}

protected function clearAll():void
{
	mobile.text = "";
	cname.text = "";
	emil.text = "";
	address.text = "";
	statere.selectedIndex = -1;
	cityre.selectedIndex = -1;
	nar.text = "";
	ac.text = "";
	retyp.selectedIndex = -1;
	cu.text = "";
	de.text = "";
	na.text = "";
}

protected function sendsuccess(event:ResultEvent):void
{
	Mask.close();
	clearAll();
	Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
}
protected function sendfail(event:FaultEvent):void
{
	Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
}

protected function submit_clickHandler(event:MouseEvent):void
{
	info = new Object();
	info["date"] = new Date();
	info["ci"] = customerID;
	info["phno"] = phoneNumber;
	info ["account"]=ac.text;
	info["cname"]=cu.text;
	info["req"]=retyp.text;
	info["det"]=de.text;
	info["n"]=na.text;
	save.send(info);
}

protected function savesuccess(event:ResultEvent):void
{
	clearAll();
	Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
}
protected function  savefail(event:FaultEvent):void
{
	Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
}
public var checkXML:XML;
public var customerID:String;
public var phoneNumber:String;
protected function checksuccess(event:ResultEvent):void
{
	Mask.close();
	checkXML=event.result as XML;
	if(checkXML.record.length() > 0)
	{
		cu.text=checkXML.record.customerName;
		customerID = checkXML.record.customerId;
		phoneNumber = checkXML.record.phNumber;
		Alert.show("Account Number Exists..!!!", "Success");
	}
	else
	{
		cu.text = "";
		customerID = "";
		phoneNumber = "";
		Alert.show("Account Number Does Not Exists...!!!", "Failure");
	}
}
protected function checkfail(event:FaultEvent):void
{
//  	Alert.show("Record Not Inserted");
}
protected function valid_clickHandler(event:MouseEvent):void
{
	info=new Object();
	info["date"]=new Date();
	info["value"]=ac.text;
	check.send(info);
}

protected function statere_changeHandler(event:ListEvent):void
{
	cityre.dataProvider = groupDB.country.(@name == "India").state.(@name == statere.selectedLabel).city;
}

